/*module:DataModel
return {
	option:[Object]
	result:String       OK|nodata
	timeline:[Array]
	xdata:{
	"title":[array]
	}
}
---------------------------------------------------------------------------------------------------*/
define(function(){
	var config;
	var base64 = new Base64();
	var title={
		"new":"新增",
		"total":"总量",
		"launch":"启动次数",
		"active":"活跃量",
		"inst":"安装",
		"unint":"卸载",
		"updt":"更新",
		"retention":"留存",
		"crash":"崩溃日志",
		"1":"ThinkSky",
		"2":"QQPCMgr",
		"3":"QZone",
		"4":"Baidu",
                "5":"PCOnline",
                "6":"PCHome",
                "7":"NewHua"
	};
	/*jsonp*/
	function getJsonp(callback){
		var _data = base64.encode(JSON.stringify(config));
		// debug.log(base64.decode(_data));
		window.callback = callback;
		$.ajax({
			url:config.url,
			dataType:"jsonp",
     			jsonp:"callback",
			data:{"req":_data},
			success:function(msg){
			}
		});
	};

	//日期排序
	function compareInt(s, e){
		var iNum1 = (new Date(s.replace(/-/g,'/'))).getTime();
		var iNum2 = (new Date(e.replace(/-/g,'/'))).getTime();
		if(iNum1 < iNum2){
			return -1;
		}else if(iNum1 > iNum2){
			return 1;
		}else{
			return 0;
		}
	}

	//number
	function compareIntN(s,e){
		s = parseInt(s);
		e = parseInt(e);
		if(s < e){
			return -1;
		}else if(s > e){
			return 1;
		}else{
			return 0;
		}
	}

	return {
		get:function(obj){
			var _self={};
			config = obj.conf;
			getJsonp(function(msg){

				if(msg.hasOwnProperty("options") && msg.options.hasOwnProperty("v")){
					msg.options.v.sort(compareIntN);
				}
				_self.option = msg.options;
				_self.result = msg.result;

				//timeline 
				var _timeline=[];
				
				

				var _data={};
				if(config.type == "new" || config.type == "active" || config.type == "total" || config.type == "launch" || config.type == "crash"){
					//xdata
					createTiemline(msg.stats);
					var _arr =[];
					for(var i=0,max=_timeline.length;i<max;i++){
						_arr.push(msg.stats[_timeline[i]] || 0);
					}
					_data[title[config.type]] = _arr;
					
				}else{
					for(var i in msg.stats){
                                                if(title[i]){
                                                    _data[title[i]] = c_arr(msg.stats[i]);
                                                }else{
                                                    _data[i] = c_arr(msg.stats[i]);
                                                }
						
					}
				}

				function createTiemline(obj){
					for(var i in obj){
						_timeline.push(i);
					}
					_timeline.sort(compareInt);
				}

				function c_arr(obj){
					if(_timeline.length==0){
						createTiemline(obj);
					}
					var _arr = [];
					for(var i=0,max=_timeline.length;i<max;i++){
						_arr.push(obj[_timeline[i]]);
					}
					
					return _arr;
				}

				_self.timeline = _timeline;
				_self.xdata = _data;
				try{
					console.log(_self);
				}catch(e){}
				obj.callback(_self);
			});
		},
		getRetention:function(obj){
			config = obj.conf;
			getJsonp(function(msg){
				if(msg.hasOwnProperty("options") && msg.options.hasOwnProperty("v")){
					msg.options.v.sort(compareIntN);
				}
				var _timeline=[];
				for(var i in msg.stats){
					_timeline.push(i);
				}
				_timeline.sort(compareInt);
				var _data = {
					option:msg.options,
					timeline:_timeline,
					xdata:msg.stats
				};
				console.log(_data);
				obj.callback(_data);
			});
		},
		getNative:function(obj){
			config = obj.conf;
			getJsonp(function(msg){
				console.log(msg);
				obj.callback(msg);
			});
		},
		getDlapp:function(obj){
			config = obj.conf;
			getJsonp(function(msg){
				if(msg.hasOwnProperty("options") && msg.options.hasOwnProperty("v")){
					msg.options.v.sort(compareIntN);
				}
				var _timeline=[];
				for(var i in msg.stats){
					_timeline.push(i);
				}
				_timeline.sort(compareInt);
				var _data={
					timeline:_timeline,
					xdata:msg.stats,
					option:msg.options
				};
				obj.callback(_data);
			});
		}
	}

});